package com.jyr.ohs.controller;

import com.jyr.ohs.common.PageQueryUtils;
import com.jyr.ohs.common.UnifiedJsonReturn;
import com.jyr.ohs.entity.*;
import com.jyr.ohs.mapper.HomeworkMapper;
import com.jyr.ohs.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;

/**
 * @author ZhongJing </p>
 * @Description </p>
 * @date 2021/4/15 11:49 上午 </p>
 */
@Slf4j
@RestController
@RequestMapping(value = "/teacher")
public class TeacherController {

    @Autowired
    TeacherService teacherService;

    /**
     * 跳转到教师登陆页面
     *
     * @return 返回视图模型
     */
    @GetMapping(value = "/toLogin")
    public ModelAndView toLogin() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("teacher/login");
        return mav;
    }

    @GetMapping(value = "/shutdown")
    public ModelAndView exit(HttpSession session) {
        ModelAndView mav = new ModelAndView();

        session.removeAttribute("teacher");

        mav.setViewName("common/all_login");

        return mav;
    }

    /**
     * 跳转到教师主页
     *
     * @return 返回视图模型
     */
    @GetMapping(value = "/main")
    public ModelAndView main() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("teacher/main");
        return mav;
    }

    /**
     * 教师登陆
     *
     * @return 统一JSON格式
     */
    @PostMapping(value = "/login")
    public UnifiedJsonReturn<Teacher> login(@RequestParam("username") Integer id,
                                            @RequestParam("password") String password,
                                            HttpSession session) {
        // 封装对象
        Teacher teacher = new Teacher(id, null, password);

        // 调用service层方法
        UnifiedJsonReturn<Teacher> jsonReturn = teacherService.login(teacher);

        teacher = jsonReturn.getData();

        if (teacher != null) {
            session.setAttribute("teacher", teacher);
        }

        return jsonReturn.data(null);
    }

    /**
     * 给教师表添加一些测试数据
     *
     * @return true成功，false失败
     */
    @GetMapping(value = "/data")
    public boolean generateData(@RequestParam(defaultValue = "5000") Integer num,
                                @RequestParam(defaultValue = "123456") String password) {
        return teacherService.addList(num, password);
    }

    /**
     * 根据教师ID查询该教师代的所有课程
     *
     * @param id 教师id
     * @return 课程集合
     */
    @GetMapping(value = "/showCourse")
    public List<Course> showCourse(Integer id) {
        return teacherService.getAllCourseById(id);
    }

    /**
     * 跳转到课程列表
     */
    @GetMapping(value = "/toCourseList")
    public ModelAndView toCourseList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                     HttpSession session) {
        ModelAndView mav = new ModelAndView();

        // 从session域获取教师信息
        Teacher teacher = (Teacher) session.getAttribute("teacher");

        // 查询教师带的所有课程
        List<Course> courses = teacherService.getAllCourseById(teacher.getId());

        // 分页
        PageQueryUtils<Course> pageQueryUtils = new PageQueryUtils<>(pageNum, pageSize, courses);

        // 把查询到的课程信息和分页信息放到request域
        mav.addObject("pageQueryUtils", pageQueryUtils);

        // 设置跳转视图
        mav.setViewName("teacher/course_list");

        return mav;
    }

    /**
     * 跳转到发布作业页面
     */
    @GetMapping(value = "/toPublishHomework")
    public ModelAndView toPublishJob(Integer cid) {
        ModelAndView mav = new ModelAndView();

        // 把课程编号放到request域
        mav.addObject("cid", cid);

        // 设置跳转视图
        mav.setViewName("teacher/publish_homework");

        return mav;
    }

    /**
     * 教师发布作业
     *
     * @param file  作业文件
     * @param issue 作业信息
     */
    @PostMapping(value = "/publishHomework")
    public ModelAndView publishHomework(MultipartFile file, Issue issue) {
        ModelAndView mav = new ModelAndView();

        log.info("作业信息:" + issue);
        log.info("作业文件原始名称：" + file.getOriginalFilename());

        boolean flag = teacherService.publishHomework(issue, file);

        if (flag) {
            mav.setViewName("redirect:/teacher/toCourseList");
        } else {
            mav.setViewName("redirect:/teacher/toPublishHomework?cid=" + issue.getCId());
        }

        return mav;
    }

    /**
     * 去作业列表
     */
    @GetMapping(value = "/toHomeworkList")
    public ModelAndView toHomeworkList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                       HttpSession session) {
        ModelAndView mav = new ModelAndView();

        Teacher teacher = (Teacher) session.getAttribute("teacher");

        List<Homework> homeworkList = teacherService.getHomeworkListByTid(teacher.getId());

        // 分页
        PageQueryUtils<Homework> pageQueryUtils = new PageQueryUtils<>(pageNum, pageSize, homeworkList);

        mav.addObject("pageQueryUtils", pageQueryUtils);

        mav.setViewName("teacher/homework_list");

        return mav;
    }

    /**
     * 去已交作业列表页面
     * @param cid 课程id
     */
    @GetMapping(value = "/toSubmitWork")
    public ModelAndView toSubmitWork(Integer cid) {

        ModelAndView mav = new ModelAndView();

        List<SubmitWork> submitWorkList = teacherService.getSubmitWorkListByCid(cid);

        mav.addObject("submitWorkList", submitWorkList);

        mav.setViewName("teacher/stu_homework_list");

        return mav;
    }
}
